<template>
	<view>
		<scroll-view scroll-y="true" style="height: 500rpx" v-if='isAdd'>
			<uni-table ref="table" border stripe type="selection" @selection-change="hangleChange" emptyText="暂无更多数据">
				<uni-tr>
					<uni-th align="center">借款单号</uni-th>
					<uni-th align="center">借款金额</uni-th>
					<uni-th align="center">借款日期</uni-th>
					<uni-th align="center">姓名</uni-th>
					<uni-th align="center">已还款金额(包含未审批通过的)</uni-th>
					<uni-th align="center">状态</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in oastoList" :key="index">
					<uni-td align="center">{{ item.ti002 }}</uni-td>
					<uni-td align="center">{{ item.ti004 }}</uni-td>
					<uni-td align="center">{{ item.ti005 }}</uni-td>
					<uni-td align="center">{{ item.ti008Name }}</uni-td>
					<uni-td align="center">{{ item.allRepaidAmount }}</uni-td>
					<uni-td align="center">
						<u-tag v-if="item.flowState == 1" text="等待审核"></u-tag>
						<u-tag type="success" v-else-if="item.flowState == 2" text="审核通过"></u-tag>
						<u-tag type="error" v-else-if="item.flowState == 3" text="审核驳回"></u-tag>
						<u-tag type="warning" v-else-if="item.flowState == 4" text="流程撤回"></u-tag>
						<u-tag type="info" v-else-if="item.flowState == 5" text="审核终止"></u-tag>
						<u-tag type="info" v-else text="等待提交"></u-tag>
					</uni-td>
				</uni-tr>
			</uni-table>
		</scroll-view>
		<view v-if='isAdd'>
			<u-button class="buttom-btn" type="primary" :ripple="true" plain @click.stop="handleAdd">
				<u-icon label="选择后保存数据到下方表格" size="40" name="plus"></u-icon>
			</u-button>
		</view>
		<uni-table border stripe emptyText="暂无更多数据">
			<uni-tr>
				<uni-th align="center">借款单号</uni-th>
				<uni-th align="center">借款金额</uni-th>
				<uni-th align="center">借款日期</uni-th>
				<uni-th align="center">姓名</uni-th>
				<uni-th align="center">已还款金额</uni-th>
				<uni-th align="center">退还预支款金额</uni-th>
				<uni-th align="center">备注</uni-th>
				<uni-th align="center">操作</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in tableData" :key="index">
				<uni-td align="center">{{ item.to003 }}</uni-td>
				<uni-td align="center">{{ item.to004 }}</uni-td>
				<uni-td align="center">{{ item.to005 }}</uni-td>
				<uni-td align="center">{{ item.to006 }}</uni-td>
				<uni-td align="center">{{ item.to008 }}</uni-td>
				<uni-td align="center">
					<u-input v-model="item.to009"></u-input>
				</uni-td>
				<uni-td align="center">
					<u-input v-model="item.to007"></u-input>
				</uni-td>
				<uni-td>
					<view class="uni-group">
						<button class="uni-button" size="mini" type="warn" @click="hadleDel(index)">删除</button>
					</view>
				</uni-td>
			</uni-tr>
		</uni-table>
		<view style=" height: 100rpx "></view>
		<view class="flowBefore-actions">
			<u-button class="buttom-btn" type="primary" @click.stop="selfHandleRequest" v-if='isAdd'>
				确认
			</u-button>
			<u-button class="buttom-btn" type="primary" @click.stop="geBack" v-if='!isAdd'>
				返回
			</u-button>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import request from "@/utils/request";
	export default {
		data() {
			return {
				oastoList: [],
				tableData: [],
				result: [],
				ta005: null,
				oastoListVisible: false,
				isAdd: true,
			};
		},
		onShow() {
			this.init()
		},
		methods: {
			init() {
				const refundObjectStr = uni.getStorageSync('refundObject');
				const refundObject = JSON.parse(refundObjectStr);
				const ta005 = refundObject.ta005; //人员id
				this.isAdd = refundObject.isAdd
				request({
					url: `/api/SubDev/Oasti/boxList?ti008=${ta005}`,
					method: "GET",
				}).then(res => {
					this.oastoList = res.data.list;
					this.tableData = refundObject.oastoList;
				})
			},
			hangleChange(e) {
				const list = e.detail.index
				const result = [];
				list.forEach(index => {
					if (this.oastoList[index] !== undefined) {
						result.push(this.oastoList[index]);
					}
				});
				this.result = result
			},
			hadleDel(index) {
				this.tableData.splice(index, 1);
			},
			handleAdd() {
				this.result.forEach((e) => {
					// 判断借款单号是否存在
					if (this.tableData.some((o) => o.to003 === e.ti002)) {
						return this.$refs.uToast.show({
							title: `借款单号${e.ti002}已存在!`,
							type: "error",
						});
					}
					const item = {
						to003: e.ti002, //借款单号
						to004: e.ti004, //借款金额
						to005: e.ti005, //借款日期
						to006: e.ti008, //姓名
						to008: e.allRepaidAmount, //已还款金额
						to009: this.jnpf.toDecimal(
							parseFloat(e.ti004 || 0) - parseFloat(e.allRepaidAmount || 0)
						), //还款金额
					};
					this.tableData.push(item);
				});
				this.$refs.table.clearSelection()
			},
			geBack() {
				uni.navigateBack();
			},
			selfHandleRequest() {
				if (!this.tableData || this.tableData.length === 0) {
					return this.$refs.uToast.show({
						title: `数据不能为空！`,
						type: "error",
					});
				}
				for (let i = 0; i < this.tableData.length; i++) {
					var e = this.tableData[i];
					if (!e.to003) {
						return this.$refs.uToast.show({
							title: `第${i + 1}条数据的借款单号不能为空！`,
							type: "error",
						});

					}
					if (!e.to009 || parseFloat(e.to009) <= 0) {
						return this.$refs.uToast.show({
							title: `退还预支款金额不能小于0！`,
							type: "error",
						});
					}
					var to004 = parseFloat(e.to004 || 0); //借款金额
					var to008 = parseFloat(e.to008 || 0); //已还款金额
					var diffMoney = (to004 - to008).toFixed(2);
					if (parseFloat(e.to009) > diffMoney) {
						return this.$refs.uToast.show({
							title: `退还预支款金额不能大于[借款金额-已还款金额（包含已制单未审核）]！`,
							type: "error",
						});
					}
				}
				uni.showToast({
					title: '操作成功',
					complete: () => {
						setTimeout(() => {
							uni.$emit("updaterefund", this.tableData);
							uni.removeStorageSync('refundObject');
							uni.navigateBack();
						}, 1500);
					},
				});
			}


		},
	};
</script>